package scene.scenegraph;

import java.util.Stack;

import scene.shape.Shape;
import util.Matrix4f;

public class ShapeNode extends SceneGraphNode {
	
	protected Shape shape;
	
	public ShapeNode(Shape shape, SceneGraphNode parent){
		super(parent);
		this.shape = shape;
	}

	@Override
	public NodeType getType() {
		return NodeType.SHAPE;
	}

	@Override
	public void traverse(Stack<Matrix4f> stack) {
		Matrix4f totalTransform = multiplyMatrices(stack);
		shape.setTransformationMatrix(totalTransform);
		for(SceneGraphNode node : children){
			node.traverse(stack);
		}
	}

	private Matrix4f multiplyMatrices(Stack<Matrix4f> stack) {
		Matrix4f total = Matrix4f.identity();
		Stack<Matrix4f> temp = new Stack<Matrix4f>();
		while(!stack.isEmpty()){
			Matrix4f top = stack.pop();
			total = top.multiply(total);
			temp.push(top);
		}
		//restore the stack
		while(!temp.isEmpty()){
			stack.push(temp.pop());
		}
		return total;
	}
	
	

}
